package com.se2.antiqueexhibition.Controller;

import com.se2.antiqueexhibition.Service.AdminCulturalRelicService;
import com.se2.antiqueexhibition.Service.RelicCategoryService;
import com.se2.antiqueexhibition.pojo.CulturalRelic;
import com.se2.antiqueexhibition.pojo.RelicCategory;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/admin/culturalRelic")
public class AdminCulturalRelicController {

    @Resource
    private AdminCulturalRelicService adminCulturalRelicService;

    @Resource
    private RelicCategoryService relicCategoryService;

    /**
     * 显示文物列表页面
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<CulturalRelic> relics = adminCulturalRelicService.getAllRelics();
        model.addAttribute("relics", relics);
        return "culturalRelic-management";
    }

    /**
     * 显示添加文物表单页面
     */
    @GetMapping("/add")
    public String showAddForm(Model model) {
        CulturalRelic relic = new CulturalRelic();
        relic.setStatus("in_storage");
        relic.setCreateTime(LocalDateTime.now());
        model.addAttribute("relic", relic);
        model.addAttribute("isEdit", false);
        // 获取所有分类供选择
        List<RelicCategory> categories = relicCategoryService.getAllCategories();
        model.addAttribute("categories", categories);
        return "culturalRelic-form";
    }

    /**
     * 显示编辑文物表单页面
     */
    @GetMapping("/edit")
    public String showEditForm(@RequestParam int id, Model model) {
        CulturalRelic relic = adminCulturalRelicService.getRelicById(id);
        if (relic == null) {
            model.addAttribute("message", "文物不存在");
            model.addAttribute("success", false);
            return "redirect:/admin/culturalRelic/list";
        }
        model.addAttribute("relic", relic);
        model.addAttribute("isEdit", true);
        // 获取所有分类供选择
        List<RelicCategory> categories = relicCategoryService.getAllCategories();
        model.addAttribute("categories", categories);
        return "culturalRelic-form";
    }

    /**
     * 添加文物
     */
    @PostMapping("/add")
    public String add(@ModelAttribute CulturalRelic relic, Model model) {
        try {
            // 验证名称不为空
            if (relic.getName() == null || relic.getName().trim().isEmpty()) {
                model.addAttribute("message", "文物名称不能为空");
                model.addAttribute("success", false);
                model.addAttribute("relic", relic);
                model.addAttribute("isEdit", false);
                List<RelicCategory> categories = relicCategoryService.getAllCategories();
                model.addAttribute("categories", categories);
                return "culturalRelic-form";
            }
            
            // 设置默认值
            if (relic.getCreateTime() == null) {
                relic.setCreateTime(LocalDateTime.now());
            }
            if (relic.getStatus() == null || relic.getStatus().trim().isEmpty()) {
                relic.setStatus("in_storage");
            }
            
            adminCulturalRelicService.addRelic(relic);
            model.addAttribute("message", "文物添加成功");
            model.addAttribute("success", true);
            return "redirect:/admin/culturalRelic/list";
        } catch (Exception e) {
            model.addAttribute("message", "文物添加失败：" + e.getMessage());
            model.addAttribute("success", false);
            model.addAttribute("relic", relic);
            model.addAttribute("isEdit", false);
            List<RelicCategory> categories = relicCategoryService.getAllCategories();
            model.addAttribute("categories", categories);
            return "culturalRelic-form";
        }
    }

    /**
     * 更新文物
     */
    @PostMapping("/update")
    public String update(@ModelAttribute CulturalRelic relic, Model model) {
        try {
            // 验证名称不为空
            if (relic.getName() == null || relic.getName().trim().isEmpty()) {
                model.addAttribute("message", "文物名称不能为空");
                model.addAttribute("success", false);
                model.addAttribute("relic", relic);
                model.addAttribute("isEdit", true);
                List<RelicCategory> categories = relicCategoryService.getAllCategories();
                model.addAttribute("categories", categories);
                return "culturalRelic-form";
            }
            
            // 检查文物是否存在
            CulturalRelic existingRelic = adminCulturalRelicService.getRelicById(relic.getId());
            if (existingRelic == null) {
                model.addAttribute("message", "文物不存在");
                model.addAttribute("success", false);
                return "redirect:/admin/culturalRelic/list";
            }
            
            // 如果创建时间为空，保留原有时间
            if (relic.getCreateTime() == null) {
                relic.setCreateTime(existingRelic.getCreateTime());
            }
            
            // 如果状态为空，设置默认状态
            if (relic.getStatus() == null || relic.getStatus().trim().isEmpty()) {
                relic.setStatus("in_storage");
            }
            
            adminCulturalRelicService.updateRelic(relic);
            model.addAttribute("message", "文物更新成功");
            model.addAttribute("success", true);
            return "redirect:/admin/culturalRelic/list";
        } catch (Exception e) {
            model.addAttribute("message", "文物更新失败：" + e.getMessage());
            model.addAttribute("success", false);
            model.addAttribute("relic", relic);
            model.addAttribute("isEdit", true);
            List<RelicCategory> categories = relicCategoryService.getAllCategories();
            model.addAttribute("categories", categories);
            return "culturalRelic-form";
        }
    }

    /**
     * 删除文物
     */
    @GetMapping("/delete")
    public String delete(@RequestParam int id, Model model) {
        try {
            adminCulturalRelicService.deleteRelic(id);
            model.addAttribute("message", "文物删除成功");
            model.addAttribute("success", true);
        } catch (Exception e) {
            model.addAttribute("message", "文物删除失败：" + e.getMessage());
            model.addAttribute("success", false);
        }
        return "redirect:/admin/culturalRelic/list";
    }
}

